package com.example.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.domain.Category;
import com.example.domain.Dish;
import com.example.domain.Setmeal;
import com.example.mapper.CategoryMapper;
import com.example.service.ICategoryService;
import com.example.service.IDishService;
import com.example.service.ISetmealService;
import com.example.service.ex.CategoryException.CategoryNotException;
import com.example.service.ex.CategoryException.DishException;
import com.example.service.ex.DeleteException;
import com.example.service.ex.InsertException;
import com.example.service.ex.UpdateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 分类业务
 */
@Service
public class CategoryServiceImpl implements ICategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private IDishService dishService;
    @Autowired
    private ISetmealService setmealService;
    @Override
    public void save(Category category) {
        //判断添加分类的名称是否存在
        LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Category::getName,category.getName());
        Category category1 = categoryMapper.selectOne(lambdaQueryWrapper);
        if (category1!=null){
            throw new CategoryNotException("菜品/套餐分类已存在");
        }
        //添加到数据库
        int insert = categoryMapper.insert(category);
        if (insert!=1){
            throw new InsertException("添加数据时出现未知的异常");
        }
    }

    @Override
    public Page<Category> page(Integer page, Integer pageSize) {
        //创建分页构造器
        Page<Category> pages = new Page<>(page,pageSize);
        //创建条件构造器
        LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //根据sort字段进行升序
        lambdaQueryWrapper.orderByAsc(Category::getSort);
        //使用MP方法执行
        categoryMapper.selectPage(pages,lambdaQueryWrapper);
        return pages;
    }

    @Override
    public void remove(Long id) {
        //根据分类id查询出相关的菜品
        List<Dish> byCategoryId = dishService.getByCategoryId(id);
        //判断是否查询到数据，null报异常
        if (byCategoryId!=null&&byCategoryId.size()>0){
            throw new DishException("您删除的分类绑定了菜品");
        }
        //根据分类id查询出相关的套餐
        List<Setmeal> byCategoryId1 = setmealService.getByCategoryId(id);
        if (byCategoryId1!=null&&byCategoryId1.size()>0){
            throw new DishException("您删除的分类绑定了套餐");
        }
        //使用MP的方法删除数据
        int i = categoryMapper.deleteById(id);
        //判断删除是否成功
        if (i!=1){
            throw new DeleteException("删除数据时出现未知的异常");
        }
    }
    @Override
    public void update(Category category) {
        //根据分类id查询出相关的菜品
        List<Dish> byCategoryId = dishService.getByCategoryId(category.getId());
        //判断是否查询到数据，null报异常
        if (byCategoryId!=null&&byCategoryId.size()>0){
            throw new DishException("您修改的分类绑定了菜品");
        }
        //根据分类id查询出相关的套餐
        List<Setmeal> byCategoryId1 = setmealService.getByCategoryId(category.getId());
        if (byCategoryId1!=null&&byCategoryId1.size()>0){
            throw new DishException("您修改的分类绑定了套餐");
        }
        //使用MP进行修改
        int i = categoryMapper.updateById(category);
        if (i!=1){
            throw new UpdateException("数据更新时产生未知的异常");
        }
    }

    @Override
    public List<Category> list(Integer type) {
        //创建条件构造器
        LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //创建条件
        lambdaQueryWrapper.eq(type!=null,Category::getType,type);
        lambdaQueryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
        //使用MP的相关方法查询数据
        List<Category> categories = categoryMapper.selectList(lambdaQueryWrapper);
        return categories;
    }

    @Override
    public Category getById(Long id) {
        //根据id查询数据
        Category category = categoryMapper.selectById(id);
        return category;
    }
}
